import { ipcMain } from "electron";
import { BrowserWindow } from "electron";
import { store } from "../../renderer/store";


let i = 0;
let flag = false;

ipcMain.on("sendMsg", (e, data) => {
    // console.log("e", e);
    console.log("data", data);
    e.sender.send("reply", "主进程已经收到消息");

    // 在主进程中.
    // let win = BrowserWindow.fromId(1);
    let win = BrowserWindow.getFocusedWindow(); // 获取当前鼠标聚焦的窗口
    win.setSize(1080, 680);
    win.setResizable(false);
    // win.setMinimumSize(826, 505);
    // win.setMaximumSize(1080, 680)
    win.center();
});

ipcMain.on("logOut", (e, data) => {
    e.sender.send("reply", "ipcMain退出登录");
    // 在主进程中.
    let win = BrowserWindow.getFocusedWindow(); // 获取当前鼠标聚焦的窗口
    win.setResizable(true);
    win.setSize(758, 420);
    win.center();
    win.setResizable(false);
});

ipcMain.on("MAX_WIN", () => {
    console.log("MAX_WIN");
    let win = BrowserWindow.getFocusedWindow(); // 获取当前鼠标聚焦的窗口
    // console.log("窗口是否最大化", win.isMaximized(), i++);
    // win.isMaximized()
    // win.maximize();
    // win.minimize()
    if (!flag) {
        win.maximize();
        flag = !flag;
    } else {
        win.unmaximize();
        flag = !flag;
    }
});

ipcMain.on("MIN_WIN", () => {
    console.log("MIN_WIN");
    let win = BrowserWindow.getFocusedWindow(); // 获取当前鼠标聚焦的窗口
    win.minimize();
});

ipcMain.on("CLOSE_WIN", () => {
    console.log("CLOSE_WIN");
    let win = BrowserWindow.getFocusedWindow(); // 获取当前鼠标聚焦的窗口
    win.close();
});

ipcMain.on("reload", () => {
    console.log("reload");
    let win = BrowserWindow.getFocusedWindow(); // 获取当前鼠标聚焦的窗口
    const contents = win.webContents;
    // console.log(contents);
    contents.reload();
});

ipcMain.on("openNewWin", () => {
    let win2 = new BrowserWindow({
        height: 420,
        width: 758,
        useContentSize: true,
        // resizable: false,
        maximizable: false,
        // frame: false,
        transparent: true,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false
        },
        skipTaskbar: false
    });

    win2.loadURL(`http://localhost:9080`);
    win2.webContents.on('did-finish-load', (event) => {
        console.log("did-finish-load");
        win2.webContents.send("toNews", "win222222");
    })

})

